/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package javafx179labs.nez.hr.bean;

import javafx179labs.nez.hr.dao.file.HRDao;
import javafx179labs.nez.hr.dao.file.HRDaoFactory;
import javafx179labs.nez.hr.dao.file.HRDaoFileImpl;
import java.util.List;


/**
 *
 * @author civil
 */
public class Enterprise {
    /*static {
        HRDaoFileImpl.setPath("/home/civil/NetBeansProjects/dbgui/src/dbui/hr/data/");
    }*/

    private static HRDao hrdao;// = HRDaoFactory.getInstance();
    private static List<Employe> employes;// = hrdao.getEmployes();
    private static List<Department> departments;// = hrdao.getDepartments();
    private static List<Post> posts;// = hrdao.getPosts();
    private static List<Task> tasks;// = hrdao.getTasks();


    public static void setBase (String basePath) {
        HRDaoFileImpl.setPath(basePath);
        hrdao = HRDaoFactory.getInstance();
        employes = hrdao.getEmployes();
        departments = hrdao.getDepartments();
        posts = hrdao.getPosts();
        tasks = hrdao.getTasks();
    }

    public static List<Department> getDepartments() {
        return departments;
    }

    public static List<Employe> getEmployes() {
        return employes;
    }

    public static List<Post> getPosts() {
        return posts;
    }

    public static List<Task> getTasks() {
        return tasks;
    }

    public static String getHisPostName(Employe employe) {
        return posts.get(posts.indexOf(new Post(employe.getPostId(), null))).getName();
    }

    public static String getHisDepartmentName(Employe employe) {
        return departments.get(departments.indexOf(new Department(employe.getDepartmentId(), null))).getName();
    }
    
    public static String getPerformerName(Task task) {
        Integer id = task.getPerformer();
        if (id == null) {
            return null;
        } else {
            return employes.get(employes.indexOf(new Employe(id, null, null, null, null))).getName();
        }
    }

    public static void add(Employe employe) {
        employes.add(employe);
        hrdao.setEmployes(employes);
    }

    public static void add(Department department) {
        departments.add(department);
        hrdao.setDepartments(departments);
    }

    public static void add(Post post) {
        posts.add(post);
        hrdao.setPosts(posts);
    }

    public static void add(Task task) {
        tasks.add(task);
        hrdao.setTasks(tasks);
    }

    public static void takeBack(Employe employe) {
        employes.set(employes.indexOf(employe), employe);
        hrdao.setEmployes(employes);
    }

    public static void takeBack(Post post) {
        posts.set(posts.indexOf(post), post);
        hrdao.setPosts(posts);
    }

    public static void takeBack(Task task) {
        tasks.set(tasks.indexOf(task), task);
        hrdao.setTasks(tasks);
    }

    public static void takeBack(Department department) {
        departments.set(departments.indexOf(department), department);
        hrdao.setDepartments(departments);
    }

    public static void remove(Post post) {
        posts.remove(post);
        hrdao.setPosts(posts);
    }

    public static void remove(Department department) {
        departments.remove(department);
        hrdao.setDepartments(departments);
    }

    public static void remove(Task task) {
        tasks.remove(task);
        hrdao.setTasks(tasks);
    }

    public static void remove(Employe employe) {
        employes.remove(employe);
        hrdao.setEmployes(employes);
    }
}
